package com.example.student;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.database.DatabaseHelper;
import com.example.teacher.TeacherHome;

public class MainActivity extends AppCompatActivity {

    private EditText etUsername, etPassword;
    private Button btnLogin;

    private RadioGroup rgRole;
    private RadioButton rbStudent, rbTeacher;
    private DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);
        rgRole = findViewById(R.id.rgRole);
        rbStudent = findViewById(R.id.rbStudent);
        rbTeacher = findViewById(R.id.rbTeacher);
//        this.deleteDatabase(DatabaseHelper.DATABASE_NAME);
        // 初始化数据库
        databaseHelper = new DatabaseHelper(this);
//        SQLiteDatabase db = databaseHelper.getWritableDatabase();
//        databaseHelper.onUpgrade(db, 1,1);
//        // 手动调用 onCreate
//
//        databaseHelper.onCreate(db);
        // 设置登录按钮点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入的用户名和密码
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String role = rbStudent.isChecked() ? "student" : "teacher";
                // 验证用户名和密码
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        // 将用户名转换为整数
                        int userId = Integer.parseInt(username);

                        // 验证用户名和密码
                        if (databaseHelper.validateUser(userId, password, role)) {
                            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                            // 根据选择的角色进入不同的主页面
                            if (role.equals("student")) {
                                Intent intent = new Intent(MainActivity.this, Home.class);
                                intent.putExtra("student_id", userId);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(MainActivity.this, TeacherHome.class);
                                intent.putExtra("teacher_id", userId);
                                startActivity(intent);
                            }
                            finish();
                        } else {
                            Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    } catch (NumberFormatException e) {
                        // 如果用户名不是数字，提示用户
                        Toast.makeText(MainActivity.this, "学工号不存在", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}